﻿app.controller('FireControlController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetPlaceTree = function () {
        $scope.ParkTreeList = [];
        $scope.DataLoading = true;
        $scope.LoadingMsg = "正在加载";
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetPlaceTree'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.ParkTreeList = ret.Data;
                    setTimeout(function () {
                        $('#tree-default').jstree({
                            "types": {
                                "default": {
                                    "icon": "fa fa-folder icon-state-warning icon-lg"
                                },
                                "file": {
                                    "icon": "fa fa-file icon-state-warning icon-lg"
                                }
                            },
                            "plugins": ["types"]
                        }).bind('click.jstree', function (event) {
                            var id = $(event.target).parents('li').attr('id');
                            console.log(id);
                            var idstr = id.split('_');
                            $scope.AddModel.TypeId = idstr[0];
                            $scope.AddModel.SpaceId = idstr[1];
                            $scope.AddModel.SpaceName = $(event.target).context.innerText;
                            console.log($scope.AddModel);
                            $scope.$apply();
                            $scope.GetSpaeMapList($scope.AddModel.TypeId, $scope.AddModel.SpaceId);

                        });
                    }, 1000);

                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.SubmitSpaceMap = function (model) {
        if (model.SpaceName == null || model.SpaceName == '') {
            alertify.alert("请选择场所");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        if (files.length == 0) {
            alertify.alert("请选择图片文件");
            return false;
        }
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../FireControl/AddSpaceMap',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("上传成功！");
                $scope.GetSpaeMapList(model.TypeId, model.SpaceId);
                $('#docfiles').fileinput('clear');
                model.Remark = "";
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.GetSpaeMapList = function (type, id) {
        $scope.DataLoading = true;
        $scope.MapList = [];
        $http({
            method: 'POST',
            url: '../FireControl/GetSpaceMapList',
            data: { 'TypeId': type, 'SpaceId': id }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.MapList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.AddEquType = function (model) {
        if (model.TypeName == null || model.TypeName == '') {
            alertify.alert("请输入类型名称");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        if (files.length == 0) {
            alertify.alert("请选择图片文件");
            return false;
        }
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../FireControl/AddEquType',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("上传成功！");
                setTimeout(function () {
                    location.href = "EquTypePageList?page=" + 1;
                }, 2000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.GetEquTypePageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.TypeList = [];
        $http({
            method: 'POST',
            url: '../FireControl/GetEquTypePageList',
            data: { 'Name': model.Name, 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.TypeList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetEquTypeList = function () {
        $scope.DataLoading = true;
        $scope.TypeList = [];
        $http({
            method: 'POST',
            url: '../FireControl/GetEquTypeList',
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.TypeList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetPointList = function (Id) {
        $scope.DataLoading = true;
        $scope.PointList = [];
        $http({
            method: 'POST',
            url: '../FireControl/GetPointList',
            data: { "MapId": Id }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.PointList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetMapInfo = function (Id) {
        $scope.DataLoading = true;
        $scope.MapDetailInfo = [];
        $http({
            method: 'POST',
            url: '../FireControl/GetMapDetail',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.MapDetailInfo = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.SubmitPoint = function (model) {
        console.log(model);
        model.X = $("#h_XAxis").val();
        model.Y = $("#h_YAxis").val();
        var files = document.querySelector('input[type=file]').files;
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../FireControl/AddMapPoint',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("上传成功！");
                $scope.GetPointList(model.MapId);
                model = {};

            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.EditPoint = function (model) {
        model.X = $("#h_XAxis2").val();
        model.Y = $("#h_YAxis2").val();
        var files = document.querySelector('input[type=file]').files;
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../FireControl/EditMapPoint',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("更新成功！");
                $scope.GetPointList(model.MapId);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.doMapMouse = function (type, index) {
        if ($scope.LastId != undefined) {
            var last = document.getElementById($scope.LastId);
            if (last != null) last.style = "display:none";
        }
        $scope.LastId = type + index;
        var id = type + index;
        var detail = document.getElementById(id);
        detail.style = "display:inline";
    }

    $scope.MouseLeave = function (type, index) {
        var id = type + index;
        var detail = document.getElementById(id);
        if (detail != null) {
            detail.style = "display:none";
        }
    }

    $scope.UpdateEquList = function (model) {
        var idlist = [];
        var controls = $("[id^='chk_']");
        for (let i = 0; i < controls.length; i++) {
            if ($(controls[i]).prop("checked")) {
                var id = $(controls[i])[0].id;
                idlist.push(id.replace("chk_", ""));
            }
        }
        if (idlist.length == 0) {
            alertify.alert("未选择任何项目！");
            return false;
        }
        $http({
            method: 'POST',
            url: '../FireControl/UpdatePointInfo',
            data: { 'Ids': idlist, 'model': model }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    alertify.alert("更新成功！");
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.SelectAll = function (name) {
        if ($scope.IsAllCheck) {
            var list = $('[name="' + name + '"]');
            for (var i = 0; i < list.length; i++) {
                $(list[i]).prop("checked", true);
            }
            $scope.IsAllCheck = false;
        }
        else {
            var list = $('[name="' + name + '"]');
            for (var i = 0; i < list.length; i++) {
                $(list[i]).prop("checked", false);
            }
            $scope.IsAllCheck = true;
        }
    }

    $scope.DeleteEquType = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../FireControl/DeleteEquType',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            setTimeout(function () {
                                $scope.GetEquTypePageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.DeleteSpaceMap = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../FireControl/DeleteSpaceMap',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            setTimeout(function () {
                                $scope.GetSpaceMapList(model.TypeId, model.SpaceId);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.DeleteEquPoint = function (Id, MapId) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../FireControl/DeleteEquPoint',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            setTimeout(function () {
                                $scope.GetPointList(MapId);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.GetCurrentModel = function (point) {
        $scope.CurrentPoint = JSON.parse(JSON.stringify(point));
        $scope.CurrentPoint.EquTypeId = point.EquTypeId + "";
        $scope.CurrentPoint.ValidTime = $filter('JSONDate')($scope.CurrentPoint.CreateTime, 'yyyy-MM-dd HH:mm:ss');
        $scope.CurrentPoint.ValidDate = $filter('JSONDate')($scope.CurrentPoint.ValidDate, 'yyyy-MM-dd');
        console.log(point);
    }

    $scope.GetEquTypeDetail = function (id) {
        $scope.EquTypeDetail = {};
        $http({
            method: 'POST',
            url: '../FireControl/GetEquTypeDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EquTypeDetail = ret.Data;
                    $scope.EquTypeDetail.CreateTime = $filter('JSONDate')($scope.EquTypeDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.EditEquType = function (model) {
        if (model.TypeName == null || model.TypeName == '') {
            alertify.alert("请输入类型名称");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../FireControl/EditEquType',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("上传成功！");
                setTimeout(function () {
                    location.href = "EquTypePageList?page=" + 1;
                }, 2000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }
});